﻿(****************************************************************************
 * 
 *  Bistro FSharp Extensions Copyright © 2003-2009 Hill30 Inc
 *
 *  This file is part of Bistro Framework.
 *
 *  Bistro FSharp Extensions is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Bistro FSharp Extensions is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Bistro FSharp Extensions.  If not, see <http://www.gnu.org/licenses/>.
 *  
 ***************************************************************************)
#light

module TestControllers

    open System.Text
    open Bistro.Controllers
    open Bistro.Controllers.Descriptor
    open Bistro.Controllers.Descriptor.Data

    open Bistro.FS.Definitions
    open Bistro.FS.Inference

    [<Bind("/"); ReflectedDefinition>]
    let HomeUrlController1 (ctx: ictx) = ()

    [<Bind("/"); ReflectedDefinition>]
    let HomeUrlController2 (ctx: ictx) = ()

    [<Bind("/fs/hello/?/you"); ReflectedDefinition>]
    let HelloYouController1 (ctx: ictx) = ()

    [<Bind("/fs/hello/*/you"); ReflectedDefinition>]
    let HelloYouController2 (ctx: ictx) = ()

    [<Bind("/fs/order/world/new"); ReflectedDefinition>]
    let OrderController1 (ctx: ictx) c2 =
        let c1 = SessionValue <| (ofSsn c2) + "1"
        c1

    [<Bind("/fs/order/world/new"); ReflectedDefinition>]
    let OrderController2 (ctx: ictx) c5 =
        let c2 = SessionValue <| (ofSsn c5) + "2"
        c2

    [<Bind("/fs/order/world/new"); ReflectedDefinition>]
    let OrderController3 (ctx: ictx) c2 c4 c5 =
        let c2, c4, c5 = ofSsn c2, ofSsn c4, ofSsn c5
        let c3 = SessionValue <| c2 + c4 + c5 + "3"
        c3

    [<Bind("/fs/order/world/new"); ReflectedDefinition>]
    let OrderController4 (ctx: ictx) c1 c2 c5 =
        let c1, c2, c5 = ofSsn c1, ofSsn c2, ofSsn c5
        let c4 = SessionValue <| c1 + c2 + c5 + "4"
        c4

    [<Bind("/fs/order/world/new"); ReflectedDefinition>]
    let OrderController5 (ctx: ictx) =
        let c5 = SessionValue "5"
        c5

    [<Bind("/fs/order/world/new"); ReflectedDefinition>]
    let OrderController6 (ctx: ictx) c3 c7 =
        let c3, c7 = ofSsn c3, ofSsn c7
        let c6 = SessionValue <| c3 + c7 + "6"
        c6

    [<Bind("/fs/order/world/new"); ReflectedDefinition>]
    let OrderController7 (ctx: ictx) =
        let c7 = SessionValue 7
        c7

    [<Bind("/fs/one_little_url"); ReflectedDefinition>]
    let littleController1 (ctx: ictx) l2 =
        let l1 = "1" + l2
        l1

    [<Bind("/fs/one_little_url"); ReflectedDefinition>]
    let littleController2 (ctx: ictx) =
        let l2 = "2"
        l2

    [<Bind("/fs/little_url/more"); ReflectedDefinition>]
    let littleController3 (ctx: ictx) =
        let l3 = "3"
        l3

    [<Bind("/fs/little_url/more"); ReflectedDefinition>]
    let littleController4 (ctx: ictx) l3 l5 =
        let l4 = "4" + l3 + l5
        l4

    [<Bind("/fs/little_url/more"); ReflectedDefinition>]
    let littleController5 (ctx: ictx) l3 =
        let l5 = "5" + l3
        l5
        
    [<Bind("/fs/parameters/{parm}"); ReflectedDefinition>]
    let parametersController (ctx: ictx) parm =
        let c_parm = parm + ""
        c_parm
        
    [<Bind("/fs/empty_parameters/{parm}"); ReflectedDefinition>]
    let emptyParametersController (ctx: ictx) parm =
        let c_parm = 
            match parm with 
            | Some p -> p
            | None -> null
            + ""
        c_parm    

    [<Bind("/fs/ssn_parameters"); ReflectedDefinition>]
    let sessionParmsController1 (ctx: ictx) c_parm =
        let d_parm = 
            match c_parm with
            | Some v -> v
            | None -> ""
            + "1"
        
        SessionValue d_parm
        
    [<Bind("/fs/ssn_parameters"); ReflectedDefinition>]
    let sessionParmsController2 (ctx: ictx) d_parm =
        let n_parm = d_parm + "2"
        n_parm
        
    [<Bind("/fs/ssn_parameters/{parm}"); ReflectedDefinition>]
    let sessionParmsController3 (ctx: ictx) parm =
        let m_parm = Some (parm + "3")
        (SessionValue m_parm) |> named "c_parm"
        
    [<FormData>]
    type somedata = {
        hello: string
        world: string
    }

    [<Bind("/fs/formParmsController1"); ReflectedDefinition>]
    let formParmsController1 (ctx: ictx) (data: somedata) =
        let d = 1
        data.world + "hi" |> named "world", d

    [<FormData(false)>]
    type somedata2 = {
        hello: string
        world: string
    }

    [<Bind("/fs/formParmsController2"); ReflectedDefinition>]
    let formParmsController2 (ctx: ictx) (data: somedata2) =
        let d = 1
        data.world + "hi" |> named "world", d

    [<Bind("/fs/formParmsController3"); ReflectedDefinition>]
    let formParmsController3 (ctx: ictx) (foo: string form) =
        let d = "1"
        foo.Value + d |> named "world", d

    [<Bind("/fs/nullParmsController"); ReflectedDefinition>]
    let nullParmsController (ctx: ictx) elem =
        (match elem with
        | Some e -> e + "value"
        | None -> "empty") |> named "outVal"
        
    [<Bind("/fs/nullParmsController/2"); ReflectedDefinition>]
    let nullParmsController2 (ctx: ictx) =
        "hello" |> named "elem"
